package com.sdry.product.common.service;
import com.sdry.basic.entity.Supplier;
import com.sdry.basic.entity.WarehousesRegionLocation;
import com.sdry.basic.entity.Container;
import com.sdry.basic.mapper.ContainerStareMapper;
import com.sdry.common.BaseAPIMessageTypeEnum;
import com.sdry.exception.BizException;
import com.sdry.product.sorting.entity.SortFrame;
import com.sdry.product.sorting.mapper.SortingStockMapper;
import com.sdry.transit.entity.TransitOrderDetail;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;
import com.sdry.basic.entity.ContainerStare;
import com.sdry.basic.entity.Material;
import com.sdry.product.common.entity.ProductInverted;
import com.sdry.product.common.mapper.ProductInvertedMapper;
import com.sdry.product.prod.entity.ResultAppList;
import com.sdry.product.sorting.mapper.SortReceivingDetailMapper;
import com.sdry.product.sorting.mapper.SortReceivingMapper;
import com.sdry.product.sorting.mapper.SortingAppServiceMapper;
import com.sdry.untils.CustomUtil;
import com.sdry.untils.UserContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * (ProductInverted)表服务实现类
 *
 * @author makejava
 * @since 2020-03-13 15:39:29
 */
@Service("productInvertedService")
@Transactional(rollbackFor = Exception.class)
public class ProductInvertedServiceImpl extends ServiceImpl<ProductInvertedMapper,ProductInverted> implements ProductInvertedService {
    @Resource
    private ProductInvertedMapper productInvertedDao;

    @Resource
    private SortingAppServiceMapper sortingAppServiceMapper;

    @Resource
    private SortReceivingDetailMapper sortReceivingDetailMapper;

    @Resource
    private SortReceivingMapper sortReceivingMapper;

    @Resource
    private ContainerStareMapper containerStareMapper;

    @Resource
    private SortingStockMapper sortingStockMapper;

    @Override
    public List<ProductInverted> queryProductInvertedPage(ProductInverted productInverted) {
        return productInvertedDao.queryProductInvertedPage(productInverted);
    }

    @Override
    public Long queryProductInvertedTotal(ProductInverted productInverted) {
        return productInvertedDao.queryProductInvertedTotal(productInverted);
    }

    @Override
    public void addProductInverted(String json,String userId) {
        Gson gson = new Gson();
        List<ResultAppList<Material>> resultAppList = gson.fromJson(json, new TypeToken<List<ResultAppList<Material>>>() {}.getType());
        for (ResultAppList<Material> materialResultAppList : resultAppList) {
            //验证库存是否存在
            CustomUtil.verifyStockContainerExceptFj(materialResultAppList.getPsId(),sortReceivingMapper);
            List<Material> materialList = materialResultAppList.getList();
            //判断容器中是否物料
            if (materialList.size() > 0) {
                //新增倒框记录
                for (Material material : materialList) {
                    ProductInverted productInverted = new ProductInverted();
                    //容器ID
                    productInverted.setContainerId(Long.valueOf(materialResultAppList.getPsId()));
                    //原来数量
                    productInverted.setUsedQuantity(material.getUserQuantity());
                    //物料ID
                    productInverted.setMaterielId(material.getMaId());
                    //现数量
                    productInverted.setQuantity(material.getQuantity());
                    productInverted.setRemark("框");
                    //操作人员
                    productInverted.setCreateBy(userId);
                    int effect = productInvertedDao.insert(productInverted);
                    if (effect <= 0){
                        throw new BizException(BaseAPIMessageTypeEnum.A094.getName());
                    }
                    //容器装载表
                    Long maId = productInvertedDao.queryContainerStareMaterial(productInverted.getContainerId(),material.getMaId());
                    //如果是空容器的话，新增装载表及分拣区库存
                    if (maId == null || maId < 0) {
                        Long affect = add(material, materialResultAppList,userId);
                        if (affect <= 0){
                            throw new BizException(BaseAPIMessageTypeEnum.A094.getName());
                        }
                    }else {
                        //存在，改数量
                        sortReceivingDetailMapper.updateContainerStareQuantity3(materialResultAppList.getPsId(),material.getMaId(),material.getQuantity(),userId);
                    }
                }
            }else {
                //删除装载表
                sortReceivingDetailMapper.deleteContainerStareQuantity2(Long.parseLong(materialResultAppList.getPsId()));
            }
        }
    }

    public Long add(Material material,ResultAppList<Material> materialResultAppList,String userId) {
        ContainerStare containerStare = new ContainerStare();
        //容器ID
        containerStare.setCid(Long.parseLong(materialResultAppList.getPsId()));
        //物料ID
        containerStare.setMaId(material.getMaId());
        //位置
        containerStare.setWrlid(0L);
        containerStare.setCsrnum(Math.toIntExact(material.getQuantity()));
        containerStare.setCsrcreateMan(userId);
        //时间
        //添加当前时间
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String createTime = dateFormat.format(now);
        containerStare.setCsrremark(createTime);
        containerStare.setCsrremark1("0");
        containerStare.setCsrremark2("FJ-01");
        Long id = productInvertedDao.addContainerStare(containerStare);
        if (id <= 0){
            throw new BizException(BaseAPIMessageTypeEnum.A094.getName());
        }
        SortFrame sortFrame = new SortFrame();
        sortFrame.setReceivingId(23L);
        //库位ID
        sortFrame.setLibraryId(0L);
        //容器ID
        sortFrame.setContainerId(containerStare.getCid());
        //接收人员
        sortFrame.setReceiveBy(Long.parseLong(userId));
        //创建人员
        sortFrame.setCreateBy(userId);
        int insert = sortingStockMapper.insert(sortFrame);
        if (insert <= 0){
            throw new BizException(BaseAPIMessageTypeEnum.A094.getName());
        }
        return id;
    }
}